package cn.hs.ph.servlet;

import cn.hs.ph.dao.impl.CommunityDaoImpl;
import cn.hs.ph.dao.impl.CourierStationDaoImpl;
import cn.hs.ph.entity.Community;
import cn.hs.ph.entity.CourierStation;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.bind.v2.model.core.ID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/courierStation")
public class CourierStationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码方式
        //获取action
        String action = req.getParameter("action");
        //根据action判断
        if ("selectAll".equals(action)) {
            //读取信息
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String communityName = req.getParameter("communityName");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationStatus = req.getParameter("courierStationStatus");
            //封装到map
            Map<String, String> map = new HashMap<>();
            map.put("courierStationName", courierStationName);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("communityName", communityName);
            map.put("courierStationStatus", courierStationStatus);
            //连接数据库
            List<CourierStation> list = new CourierStationDaoImpl().selectAll(map);
            int total = new CourierStationDaoImpl().selectCount(map);
            //响应
            Map resultMap = new HashMap<>();
            resultMap.put("list", list);
            resultMap.put("total", total);
            Result result = Result.success(resultMap);

            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("updateStatus".equals(action)) {
            String id = req.getParameter("courierStationId");
            int courierStationId = Integer.parseInt(id);
            String courierStationStatus = req.getParameter("courierStationStatus");
            CourierStation courierStation = new CourierStation(courierStationId, courierStationStatus);
            int num = new CourierStationDaoImpl().updateStatus(courierStation);
            Result result = null;
            if (num != 0) {
                result = Result.success("操作成功！");
            } else {
                result = Result.error("操作失败！");
            }

            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("insert".equals(action)) {
            String time = req.getParameter("addTime");
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String id = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId = 0;
            if (id != null && !"".equals(id)) {
                courierStationCommunityId = Integer.parseInt(id);
            }
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationOrderCount = req.getParameter("courierStationOrderCount");
            String courierStationProvince = req.getParameter("courierStationProvince");
            String courierStationSort = req.getParameter("courierStationSort");
            String courierStationStatus = req.getParameter("courierStationStatus");


            CourierStation courierStation = new CourierStation(
                    courierStationAddress,
                    courierStationCity,
                    courierStationCommunityId,
                    courierStationCommunityName,
                    courierStationDistrict,
                    courierStationImage,
                    courierStationName,
                    courierStationOrderCount,
                    courierStationProvince,
                    courierStationSort,
                    courierStationStatus
            );
            int num = new CourierStationDaoImpl().insert(courierStation);
            Result result = null;
            if (num != 0) {
                result = Result.success("添加成功！");
            } else {
                result = Result.error("添加失败！");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("delete".equals(action)) {
            String id = req.getParameter("courierStationId");
            int courierStationId = Integer.parseInt(id);

            int num = new CourierStationDaoImpl().delete(courierStationId);
            Result result = null;
            if (num != 0) {
                result = Result.success("删除成功！");
            } else {
                result = Result.error("删除失败！");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("update".equals(action)) {
            String time = req.getParameter("addTime");
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            Date addTime;
            try {
                addTime = sd.parse(time);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String id = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId = 0;
            if (id != null && !"".equals(id)) {
                courierStationCommunityId = Integer.parseInt(id);
            }
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String id2 = req.getParameter("courierStationId");
            int courierStationId = 0;
            if (id2 != null && !"".equals(id2)) {
                courierStationId = Integer.parseInt(id2);
            }
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationOrderCount = req.getParameter("courierStationOrderCount");
            String courierStationProvince = req.getParameter("courierStationProvince");
            String courierStationSort = req.getParameter("courierStationSort");
            String courierStationStatus = req.getParameter("courierStationStatus");


            CourierStation courierStation = new CourierStation(
                    addTime,
                    courierStationAddress,
                    courierStationCity,
                    courierStationCommunityId,
                    courierStationCommunityName,
                    courierStationDistrict,
                    courierStationId,
                    courierStationImage,
                    courierStationName,
                    courierStationOrderCount,
                    courierStationProvince,
                    courierStationSort,
                    courierStationStatus
            );
            int num = new CourierStationDaoImpl().update(courierStation);
            Result result = null;
            if (num != 0) {
                result = Result.success("修改成功！");
            } else {
                result = Result.error("修改失败！");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
